#! /bin/bash

INSTALL_PATH=~/soft
node=master
JAVA_HOME=""
mkdir -p ${INSTALL_PATH}
cd ${INSTALL_PATH}

if [ ! -d "elasticsearch_master" ]; then
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.12.0-linux-x86_64.tar.gz
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.12.0-linux-x86_64.tar.gz.sha512
    shasum -a 512 -c elasticsearch-7.12.0-linux-x86_64.tar.gz.sha512
    tar -xzf elasticsearch-7.12.0-linux-x86_64.tar.gz
    mv elasticsearch-7.12.0 elasticsearch_master
    rm elasticsearch-7.12.0-linux-x86_64.tar.gz*
    rm elasticsearch-7.12.0-linux-x86_64.tar.gz.sha512*
fi

cd elasticsearch_master
cp config/jvm.options config/jvm.options.bak
cp config/elasticsearch.yml config/elasticsearch.yml.bak

echo "
-Xms1000m
-Xmx1000m
" >>config/jvm.options

echo "jvm.options 配置完成"

echo "
node.name: master
path.data: /root/data/master
bootstrap.memory_lock: true
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: [\"172.17.0.4\", \"172.17.0.5\"]
cluster.initial_master_nodes: [\"master\"]

node.master: true
node.data: true

xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.client_authentication: required
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
" >config/elasticsearch.yml

echo "elasticsearch.yml配置完成"
# 生成私钥和证书
expect <<EOF

    spawn ./bin/elasticsearch-certutil ca
    expect {
    "*:*" {send "\r"; exp_continue }
    }
    spawn ./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
    expect {
        "*:*" { send "\r"; exp_continue }
    }
EOF
mv elastic-stack-ca.p12 config/elastic-stack-ca.p12
mv elastic-certificates.p12 config/elastic-certificates.p12
echo "私钥和证书生成成功"
